jsx3.require("jsx3.gui.Button");

/**
* A special button prototype object designed for use with the LeftSelectPane prototype object.
*/
jsx3.Class.defineClass(
        "ux.megatron.gui.LeftSelectButton",
        jsx3.gui.Button,
        null,
        function (Button, Button_prototype) {

            /**
            * {jsx3.gui.Block} the content area into which the content for this button will be loaded.  This content area
            * is NOT serialized when saving the content out to a gui xml component file.  The loading of content into this
            * content pane should be done dynamically, on-demand.
            */
            Button_prototype.contentPane = null;

            /**
             * instance initializer
             * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
             * @param intLeft {int} left position (in pixels) of the object relative to its parent container; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
             * @param intTop {int} top position (in pixels) of the object relative to its parent container; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
             * @param intWidth {int} width (in pixels) of the object; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
             * @param strText {String} text to display in the given button; if null JSXTABLEHEADERCELL.DEFAULTTEXT is used
             */
            Button_prototype.init = function(strName, intLeft, intTop, intWidth, strText) {
                this.caret = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_caret.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                this.blank = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_caret_blank.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                this.edit = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_edit.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                this.caretedit = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_caret_edit.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                if(!strText) {
                    strText = "[button text]";
                }
                this.jsxsuper(strName, intLeft, intTop, ux.megatron.Main.getServer().getDynamicProperty("@addin@LeftSelectButton Width"), strText);
                this.setDynamicProperty("jsxwidth","@addin@LeftSelectButton Width").setDynamicProperty("jsxheight","@addin@LeftSelectButton Height").setDynamicProperty("jsxbgcolor","@Transparent BG").setDynamicProperty("jsxdisabledbgcolor","@addin@LeftSelectButton Disabled BG Color").setDynamicProperty("jsxdisabledcolor","@addin@LeftSelectButton Disabled Color").setDynamicProperty("jsxfontname","@addin@LeftSelectButton Font Name").setDynamicProperty("jsxfontsize","@addin@LeftSelectButton Font Size").setDynamicProperty("jsxfontweight","@addin@LeftSelectButton Font Weight").setDynamicProperty("jsxpadding","@addin@LeftSelectButton Padding").setDynamicProperty("jsxborder","@addin@LeftSelectButton Border").setDynamicProperty("jsxtextalign","@addin@LeftSelectButton Text Align").setDynamicProperty("jsxcolor","@addin@LeftSelectButton Color");
                var me = this;
                this.subscribe(jsx3.gui.Interactive.EXECUTE,me,me.select);
            };

            Button_prototype.onSetParent = function() {
                var ret = this.jsxsuper();
                this.setDynamicProperty("jsxdisabledbgcolor","@addin@LeftSelectButton Disabled BG Color").setDynamicProperty("jsxdisabledcolor","@addin@LeftSelectButton Disabled Color");
                try {
                    this.caret = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_caret.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                    this.blank = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_caret_blank.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                    this.edit = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_edit.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                    this.caretedit = "background-image: url(" + ux.megatron.Main.getServer().resolveURI("jsxaddin://user!Megatron/images/matrix/menu_caret_edit.gif") + "); background-repeat: no-repeat; background-position: 0px 7px;";
                }
                catch(ex) {
                    // do nothing
                }
                this.deselect();
                return ret;
            };

            /**
            * Selects this LeftSelectButton from all of the LeftSelectButtons owned by the parent LeftSelectPane
            * Automatically handles the deselection of any previously selected LeftSelectButton.
            */
            Button_prototype.select = function() {
                this.selected = true;
                this.setDynamicProperty("jsxcolor","@addin@LeftSelectButton Selected Color");
                this.setCSSOverride(this.isEdit ? this.caretedit : this.caret,false);
                var parent = this.getParent().getParent().getParent().getParent().getParent();
                parent.buttonSelected(this);
                this.repaint();
            };

            /**
            * Method to indicate to the button that the content in its content pane has gone into edit mode.  This
            * tiggles the display of the icon that indicates that it is in edit mode.
            *
            * @param bEditMode {boolean} <code>true</code> puts the button in edit mode, <code>false</code> removes the edit mode.
            */
            Button_prototype.setEditMode = function(bEditMode) {
//                jsx3.log("Setting edit mode for: " + this.getText() + " to " + bEditMode);
                this.isEdit = bEditMode;
                if(this.selected) {
                    this.setDynamicProperty("jsxcolor","@addin@LeftSelectButton Selected Color");
                    this.setCSSOverride(this.isEdit ? this.caretedit : this.caret, false);
                }
                else {
                    this.setDynamicProperty("jsxcolor","@addin@LeftSelectButton Color");
                    this.setCSSOverride(this.isEdit ? this.edit : this.blank, false);
                }
                this.repaint();
            }

            /**
            * Deselects the button.  This removes the icon and other styles that indicate that the button is selected.
            */
            Button_prototype.deselect = function() {
                this.selected = false;
                this.setDynamicProperty("jsxcolor","@addin@LeftSelectButton Color");
                this.setCSSOverride(this.isEdit ? this.edit : this.blank,false);
                this.repaint();
            }

            /**
            * Gets the content pane bound to this button, for loading content into.
            *
            * @return {jsx3.gui.Block} this button's content area, for loading content into.
            */
            Button_prototype.getContentPane = function() {
                if(!this.contentPane) {
                    this.contentPane = new jsx3.gui.Block("blkButtonContentPane", 0, 0, "100%", "100%", null);
                    this.contentPane.setDisplay(jsx3.gui.Block.DISPLAYNONE, false);
                }
                return this.contentPane;
            };
        })